package com.tensquare.sms.msg;

import com.aliyuncs.exceptions.ClientException;
import com.tensquare.sms.util.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RabbitListener(queues = "msg")
public class SmsListener {

    @Autowired
    private SmsUtil smsUtil;

    @Value("${aliyun.sms.template_code}")
    private String template_code;

    @Value("${aliyun.sms.sign_name}")
    private String sign_name;

    @RabbitHandler
    public void sendSms(Map<String,String> map){
        System.out.println("手机号："+ map.get("mobile"));
        System.out.println("验证码："+ map.get("code"));

        // * 发送短信
        //    * @param mobile 手机号
        //    * @param template_code 模板号
        //    * @param sign_name 签名
        //    * @param param 参数
        //{"code":"123456"}
        try {
            smsUtil.sendSms(map.get("mobile"), template_code, sign_name, "{\"code\":\""+map.get("code")+"\"}");

        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
